package com.yogee.topseller.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.bean.AddFastQueryBean;
import com.yogee.topseller.view.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/7/22.
 */

public class MaintainLablesAdapter extends RecyclerView.Adapter<MaintainLablesAdapter.LabelViewHolder> {
    Context mContext;

    List<InterestLabelBean.ListBean> mLabelDatas;

    public ArrayList<TextView> mSelectView = new ArrayList<>();

    private List<InterestLabelBean.LabelsBean> defBean = new ArrayList<>();

    private ArrayList<InterestLabelBean.ListBean.LabelListBean> mSelectBean = new ArrayList<>();

    public MaintainLablesAdapter(Context context) {
        this.mContext = context;
    }

    public void addData(List<InterestLabelBean.ListBean> mLabelDatas, List<InterestLabelBean.LabelsBean> defBean) {
        this.mLabelDatas = new ArrayList<>();
        this.mLabelDatas = mLabelDatas;
        this.defBean = defBean;
        for (int i = 0; i < defBean.size(); i++) {
            InterestLabelBean.ListBean.LabelListBean bean = new InterestLabelBean.ListBean.LabelListBean(defBean.get(i).getId(), defBean.get(i).getName());
            mSelectBean.add(bean);
        }
        notifyDataSetChanged();
    }

    public ArrayList<InterestLabelBean.ListBean.LabelListBean> getmSelectBean() {
        return mSelectBean;
    }

    @Override
    public void onBindViewHolder(final LabelViewHolder holder, final int position) {
        holder.mLabelType.setText(mLabelDatas.get(position).getType());
        holder.mLabelData.setData(mLabelDatas.get(position).getLabelList(), mSelectBean);
        holder.mLabelData.setOnTagClickListener(new FlowLayout.OnTagClickListener() {
            @Override
            public void TagClick(InterestLabelBean.ListBean.LabelListBean bean, TextView textView) {

//                if (mSelectBean.contains(bean)) {
//                    mSelectBean.remove(bean);
//                    textView.setBackgroundResource(R.drawable.corens_100_style_unselect);
//                    textView.setTextColor(Color.parseColor("#4a4a4a"));
//                }else{
//                    mSelectBean.add(bean);
//                    textView.setTextColor(mContext.getResources().getColor(R.color.white));
//                    textView.setBackgroundResource(R.drawable.corens_100_style_select_new);
//                }

//                if (!mSelectView.contains(textView)) {
//                    mSelectView.add(textView);
//                    mSelectBean.add(bean);
//
//                } else {
//                    textView.setBackgroundResource(R.drawable.corens_100_style_unselect);
//                    textView.setTextColor(Color.parseColor("#4a4a4a"));
//                    mSelectView.remove(textView);
//                    mSelectBean.remove(bean);
//                }


                boolean isHas = false;
//
                for (InterestLabelBean.ListBean.LabelListBean beans : mSelectBean) {
                    if (beans.getId().equals(bean.getId())) {
                        mSelectBean.remove(beans);
                        textView.setBackgroundResource(R.drawable.corens_100_style_unselect);
                        textView.setTextColor(Color.parseColor("#4a4a4a"));
                        isHas = true;
                        break;
                    }
                }
                if (!isHas) {
                    mSelectBean.add(bean);
                    textView.setTextColor(mContext.getResources().getColor(R.color.white));
                    textView.setBackgroundResource(R.drawable.corens_100_style_select_new);
                    isHas = false;
                }
            }
        });
        holder.tv_more.setVisibility(View.GONE);
//        holder.mLabelType.setText(mLabelDatas.get(position).getType());
//        holder.mLabelData.setData(mLabelDatas.get(position).getLabelList(), mSelectBean);
//
//        holder.mLabelData.setOnTagClickListener(new FlowLayout.OnTagClickListener() {
//            @Override
//            public void TagClick(InterestLabelBean.ListBean.LabelListBean bean, TextView textView) {
//
//                boolean isHas = false;
//
//                for (InterestLabelBean.ListBean.LabelListBean beans : mSelectBean) {
//                    if (beans.getId().equals(bean.getId())) {
//                        mSelectBean.remove(beans);
//                        textView.setBackgroundResource(R.drawable.corens_100_style);
//                        textView.setTextColor(Color.parseColor("#EFEFEF"));
//                        isHas = true;
//                        break;
//                    }
//                }
//                if (!isHas) {
//                    mSelectBean.add(bean);
//                    textView.setTextColor(mContext.getResources().getColor(R.color.white));
//                    textView.setBackgroundResource(R.drawable.corens_100_style_s);
//                    isHas = false;
//                }
//            }
//        });
    }

    @Override
    public LabelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LabelViewHolder holder = new LabelViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_interest_label_register, parent,
                false));
        return holder;
    }

    @Override
    public int getItemCount() {
        return mLabelDatas == null ? 0 : mLabelDatas.size();
    }

    class LabelViewHolder extends RecyclerView.ViewHolder {

        TextView mLabelType,tv_more;
        FlowLayout mLabelData;

        public LabelViewHolder(View view) {
            super(view);
            mLabelType = (TextView) view.findViewById(R.id.tv_label_title);
            mLabelData = (FlowLayout) view.findViewById(R.id.fl_label_data);
            tv_more = (TextView) view.findViewById(R.id.tv_more);
        }
    }
}